package com.ksd.common.excel;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import java.util.ArrayList;
import java.util.List;

public class XmlMovieExcel {
    public static void main(String[] args) {
        // 指定要扫描的根目录
        String rootDirPath = "E:\\政企后台电影\\USB数据资源\\电影资源";
        // 指定输出的 Excel 文件路径
        String excelFilePath = "E:\\政企后台电影\\USB数据资源\\电影汇总.xlsx";

        List<MovieInfo> allMovieInfos = new ArrayList<>();

        try {
            // 递归扫描目录下的所有 config.xml.done 文件，收集所有电影信息
            scanDirectory(new File(rootDirPath), allMovieInfos);

            // 将所有电影信息写入 Excel
            writeToExcel(allMovieInfos, excelFilePath);

            System.out.println("Excel 文件生成成功：" + excelFilePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 电影信息实体类
    static class MovieInfo {
        String movieName;
        String movieType;
        String movieUrl;
        String coverUrl;

        public MovieInfo(String movieName, String movieType, String movieUrl, String coverUrl) {
            this.movieName = movieName;
            this.movieType = movieType;
            this.movieUrl = movieUrl;
            this.coverUrl = coverUrl;
        }
    }

    // 递归扫描目录下的所有文件，收集电影信息
    private static void scanDirectory(File dir, List<MovieInfo> allMovieInfos) {
        if (dir.isDirectory()) {
            File[] files = dir.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isDirectory()) {
                        scanDirectory(file, allMovieInfos);
                    } else {
                        if (file.getName().equals("config.xml.done")) {
                            try {
                                MovieInfo movieInfo = parseXmlFile(file);
                                if (movieInfo != null) {
                                    allMovieInfos.add(movieInfo);
                                }
                            } catch (DocumentException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        }
    }

    // 解析单个 XML 文件，返回电影信息
    private static MovieInfo parseXmlFile(File xmlFile) throws DocumentException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(xmlFile);
        Element root = document.getRootElement();
        Element content = root.element("Content");

        if (content != null) {
            String movieName = content.elementText("Title");
            String movieType = content.elementText("Subcategory");
            String movieUrl = content.elementText("VideoFile");
            String coverUrl = content.elementText("ImageFile");

            String[] split = movieType.split("/");
            movieType = split[1];
            String start = "/wenhua/vod/yszx/dianying";
            movieUrl = "/电影资源" + movieUrl.substring(start.length());
            coverUrl = "/电影资源"+coverUrl.substring(start.length());
            return new MovieInfo(movieName, movieType, movieUrl, coverUrl);
        }
        return null;
    }

    // 将所有电影信息写入 Excel
    private static void writeToExcel(List<MovieInfo> allMovieInfos, String excelFilePath) throws IOException {
        try (Workbook workbook = new XSSFWorkbook()) {
            Sheet sheet = workbook.createSheet("电影信息");

            // 创建表头
            Row headerRow = sheet.createRow(0);
            String[] headers = {"电影名称", "电影类型", "电影地址", "封面地址"};
            for (int i = 0; i < headers.length; i++) {
                Cell cell = headerRow.createCell(i);
                cell.setCellValue(headers[i]);
                // 设置表头样式（可选，如加粗）
                CellStyle headerStyle = workbook.createCellStyle();
                Font font = workbook.createFont();
                font.setBold(true);
                headerStyle.setFont(font);
                cell.setCellStyle(headerStyle);
            }

            // 写入所有电影信息
            for (int i = 0; i < allMovieInfos.size(); i++) {
                MovieInfo movieInfo = allMovieInfos.get(i);
                Row row = sheet.createRow(i + 1);
                row.createCell(0).setCellValue(movieInfo.movieName);
                row.createCell(1).setCellValue(movieInfo.movieType);
                row.createCell(2).setCellValue(movieInfo.movieUrl);
                row.createCell(3).setCellValue(movieInfo.coverUrl);
            }

            // 自动调整列宽
            for (int i = 0; i < headers.length; i++) {
                sheet.autoSizeColumn(i);
            }

            // 将 Workbook 写入文件
            try (FileOutputStream fos = new FileOutputStream(excelFilePath)) {
                workbook.write(fos);
            }
        }
    }
}